package org.csu.demo.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/oauth")
public class RestAuthController {

    @Autowired
    JwtUtil jwtUtil;

    @RequestMapping("/renderGithub")
    public void renderAuthGitHub(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequestGitHub();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/callbackGithub")
    public CommonResponse<String> loginGithub(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequestGitHub();
        //false表示允许包含控制
        JSONObject jsonObject = JSONUtil.parseObj(authRequest.login(callback), false);
        JSONObject json = JSONUtil.parseObj(jsonObject.get("data"), false);
        String username = (String) json.get("nickname");
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", username);
        String token = jwtUtil.genToken(claims);
        return CommonResponse.createForSuccess(token);
    }

    private AuthRequest getAuthRequestGitHub() {
        var clientId = "Iv23liqzMOaXNbAuLX7f";
        var clientSecret = "6e5a9e81698c5ad10287c7b01b034303ab237a0b";

        /*
         var clientId = "Ov23liOBYfj6M0OdGXbh";
        var clientSecret = "6b54649d872365316005eefd9776ec5710ccac05";
        *  */

        return new AuthGithubRequest(AuthConfig.builder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .redirectUri("http://localhost:9000/oauth/callback/github")
                .build());
    }

    //登录
    @RequestMapping("/renderGitee")
    public void renderAuthGitee(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequestGitee();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    //回调地址
    @RequestMapping("/callbackGitee")
    public Object loginGitee(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequestGitee();
        //false表示允许包含控制
        JSONObject jsonObject = JSONUtil.parseObj(authRequest.login(callback), false);
        JSONObject json = JSONUtil.parseObj(jsonObject.get("data"), false);

        Map<String, Object> claims = new HashMap<>();
        claims.put("username", json.get("nickname"));
        String token = jwtUtil.genToken(claims);
        return CommonResponse.createForSuccess(token);
        //return "我的ID是：" + json.get("uuid") + "</br>昵称为："+json.get("nickname")+"</br>";
    }

    //配置信息
    private AuthRequest getAuthRequestGitee() {
        var clientId = "cf987ed1b4ebb8862835d3f835922efe4b4938f6ed45302c65f424db55a3475b";
        var clientSecret = "d037d495003daea67a8f1804ce1b9cb4a9a77158453c11d752602db4b497ebef";
        /*var clientId="591e7d04a17d1c56e21f5d785c7932357a798e4d2fa0305f77f13cc770e5f109";
        var clientSecret="95009722be65839480e0182d7365cf6c32165a6bf2cbe0b41cb70a8b38154af8";*/
        return new AuthGiteeRequest(AuthConfig.builder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .redirectUri("http://localhost:9000/oauth/callback/gitee")
                .build());
    }
}
